package com.steven.team.service

import com.alibaba.fastjson.JSONObject
import com.steven.team.config.SpringWebSocketHandler
import com.steven.team.dao.ChatDao
import com.steven.team.pojo.ChatMessage
import com.steven.team.utils.ResultUtils.INSERT_ERROR
import com.steven.team.utils.ResultUtils.SELECT_ERROR
import com.steven.team.utils.ResultUtils.SUCCESS_CODE
import com.steven.team.utils.ResultUtils.UPDATE_ERROR
import com.steven.team.utils.makeRes
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.stereotype.Service
import org.springframework.web.socket.TextMessage

@Service
class ChatServiceImp : ChatService {

    @Autowired
    val chatDao: ChatDao? = null

    @Autowired
    val webSocketHandler: SpringWebSocketHandler? = null

    override fun addMessage(sendUser: Long, receiveUser: Long, postMessage: String): JSONObject {
        val jsonObject = JSONObject()
        val chatMessage = ChatMessage()
        chatMessage.sendUser = sendUser
        chatMessage.receiveUser = receiveUser
        chatMessage.postMessage = postMessage
        chatMessage.createTime = System.currentTimeMillis()
        val result = chatDao!!.addMessage(chatMessage)
        return if (result > 0) {
            if (sendUser == receiveUser) {
                jsonObject["msg"] = "不能给自己发送消息！"
                makeRes(INSERT_ERROR, jsonObject)
            }
            val messageJSONObject = JSONObject()
            messageJSONObject["type"] = "OnLineMessage"
            messageJSONObject["data"] = chatDao.queryMessageById(chatMessage.id)
            webSocketHandler!!.sendMessageToUser(receiveUser.toString(), TextMessage(messageJSONObject.toString()))
            messageJSONObject["type"] = "OffLineMessage"
            webSocketHandler.sendMessageToUser(receiveUser.toString(), TextMessage(messageJSONObject.toString()))
            makeRes(SUCCESS_CODE, jsonObject)
        } else {
            jsonObject["msg"] = "发送失败"
            makeRes(INSERT_ERROR, jsonObject)
        }
    }

    override fun queryAll(uid: Long, friendId: Long, lastCreateTime: Long): JSONObject {
        val jsonObject = JSONObject()
        return try {
            if (lastCreateTime == 0L) {
                jsonObject["data"] = chatDao!!.queryAllBy2User(uid, friendId, Long.MAX_VALUE)
            } else {
                jsonObject["data"] = chatDao!!.queryAllBy2User(uid, friendId, lastCreateTime)
            }
            makeRes(SUCCESS_CODE, jsonObject)
        } catch (e: Exception) {
            e.printStackTrace()
            jsonObject["msg"] = "查询失败"
            makeRes(SELECT_ERROR, jsonObject)
        }
    }

    override fun queryAllUnread(uid: Long): JSONObject {
        val jsonObject = JSONObject()
        jsonObject["data"] = chatDao!!.queryAllUnreadMessage(uid)
        return makeRes(SUCCESS_CODE, jsonObject)
    }

    override fun updateMessageStatusBy2Users(receiveUser: Long, sendUser: Long): JSONObject {
        val jsonObject = JSONObject()
        return try {
            chatDao!!.updateMessageStatusBy2Users(sendUser, receiveUser)
            makeRes(SUCCESS_CODE, jsonObject)
        } catch (e: Exception) {
            jsonObject["msg"] = e.message
            makeRes(UPDATE_ERROR, jsonObject)
        }
    }

}